package io.renren.modules.job.task;

/**
 * @author rdawnx
 * @package_name io.renren.modules.job.task
 * @class_name RankDataTask.java
 * @author_email rdawnxhi@163.com
 * @JDK_version 1.8
 * @create_on 2019/07/29 16:19
 */

import io.renren.common.utils.DateUtil;
import io.renren.modules.ranking.dao.IllegalCrossingDao;
import io.renren.modules.ranking.dao.IllegalVehicleDao;
import io.renren.modules.ranking.dao.RoadAccidentsDao;
import io.renren.modules.ranking.entity.IllegalCrossingEntity;
import io.renren.modules.ranking.entity.IllegalVehicleEntity;
import io.renren.modules.ranking.entity.RoadAccidentsEntity;
import io.renren.modules.ranking.service.TrafficDataStatService;
import org.apache.commons.lang.StringUtils;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * 每月定时从下发库的违法数据库查询违法排行信息并保存到本地，以便从app端显示
 */
@Component("rankDataTask")
@SuppressWarnings("all")
public class RankDataTask {
    private static final Logger logger = LoggerFactory.getLogger(RankDataTask.class);

    @Autowired
    private TrafficDataStatService trafficDataStatService;
    @Autowired
    private IllegalCrossingDao illegalCrossingDao;
    @Autowired
    private IllegalVehicleDao illegalVehicleDao;
    @Autowired
    private RoadAccidentsDao roadAccidentsDao;

    /**
     * 数据拉取方法
     */
    public void dataFetch() {
        logger.info("rankDataTask.dataFetch开始同步" + LocalTime.now());
        //messageService.queryTotal(null);
        List<RoadAccidentsEntity> happenedList = trafficDataStatService.queryListAccidentHappened();
        List<RoadAccidentsEntity> list = changeAreaName(happenedList);
        RoadAccidentsEntity roadAccidentsEntity = roadAccidentsEntity = new RoadAccidentsEntity();
        for (RoadAccidentsEntity roadAccidentsEntity1 : list) {
            //设置序号
            roadAccidentsEntity.setRanking(roadAccidentsEntity1.getRanking());
            if(StringUtils.isNotBlank(roadAccidentsEntity1.getAreaName())){
                //设置道路名称
                roadAccidentsEntity.setRoadName(roadAccidentsEntity1.getAreaName()+":"+roadAccidentsEntity1.getRoadName());
            }else{
                roadAccidentsEntity.setRoadName(roadAccidentsEntity1.getRoadName());
            }
            //设置区域名称
            roadAccidentsEntity.setAreaName(roadAccidentsEntity1.getAreaName());
            //设置行政区划
            roadAccidentsEntity.setDistrictName(roadAccidentsEntity1.getDistrictName());
            //设置采集时间
            roadAccidentsEntity.setCollectionTime(DateUtil.calendfirstDay());
            //设置添加时间
            roadAccidentsEntity.setAddTime(new Date());
            roadAccidentsDao.save(roadAccidentsEntity);
        }

        List<IllegalCrossingEntity> crossingList = trafficDataStatService.queryListCrossing();
        List<IllegalCrossingEntity> list1 = changeAreaCode(crossingList);
        IllegalCrossingEntity illegalCrossingEntity = new IllegalCrossingEntity();
        for (IllegalCrossingEntity illegalCrossingEntity1:list1){
            //设置违法数量
            illegalCrossingEntity.setIllegalReason(illegalCrossingEntity1.getIllegalReason());
            if(StringUtils.isNotBlank(illegalCrossingEntity1.getAreaName())){
                //设置采集地点
                illegalCrossingEntity.setIllegalSites(illegalCrossingEntity1.getAreaName()+":"+illegalCrossingEntity1.getIllegalSites());
            }else{
                illegalCrossingEntity.setIllegalSites(illegalCrossingEntity1.getIllegalSites());
            }
            //设置采集机构
            illegalCrossingEntity.setCollectionOrg(illegalCrossingEntity1.getCollectionOrg());
            //设置区域名称
            illegalCrossingEntity.setAreaName(illegalCrossingEntity1.getAreaName());
            //设置区域编号
            illegalCrossingEntity.setAreaCode(illegalCrossingEntity1.getAreaCode());
            //违法序号
            illegalCrossingEntity.setRanking(illegalCrossingEntity1.getRanking());
            //设置采集时间
            illegalCrossingEntity.setCollectionTime(DateUtil.calendfirstDay());
            //设置添加时间
            illegalCrossingEntity.setAddTime(new Date());
            illegalCrossingDao.save(illegalCrossingEntity);
        }

        List<IllegalVehicleEntity> illegalParkList= trafficDataStatService.queryListIllegalPark();
        IllegalVehicleEntity illegalVehicleEntity = new IllegalVehicleEntity();
        for (IllegalVehicleEntity illegalVehicleEntity1 : illegalParkList) {
            //设置违法数量
            illegalVehicleEntity.setVehiclePlate(illegalVehicleEntity1.getVehiclePlate());
            //设置违法行为
            illegalVehicleEntity.setIllegalType(illegalVehicleEntity1.getIllegalType());
            //设置车辆所有人
            illegalVehicleEntity.setVehicleOwner(illegalVehicleEntity1.getVehicleOwner());
            //设置违法数量
            illegalVehicleEntity.setIllegalNumber(illegalVehicleEntity1.getIllegalNumber());
            //设置采集时间
            illegalVehicleEntity.setCollectionTime(DateUtil.calendfirstDay());
            //设置添加时间
            illegalVehicleEntity.setAddTime(new Date());
            //设置序号
            illegalVehicleEntity.setRanking(illegalVehicleEntity1.getRanking());
            illegalVehicleDao.save(illegalVehicleEntity);
        }

        List<IllegalVehicleEntity> illegalRedList = trafficDataStatService.queryListIllegalRed();
        IllegalVehicleEntity illegalVehicle = new IllegalVehicleEntity();
        for (IllegalVehicleEntity illegalVehicleEntity1 : illegalRedList) {
            illegalVehicle = new IllegalVehicleEntity();
            //设置违法数量
            illegalVehicle.setVehiclePlate(illegalVehicleEntity1.getVehiclePlate());
            //设置违法行为
            illegalVehicle.setIllegalType(illegalVehicleEntity1.getIllegalType());
            //设置车辆所有人
            illegalVehicle.setVehicleOwner(illegalVehicleEntity1.getVehicleOwner());
            //设置违法数量
            illegalVehicle.setIllegalNumber(illegalVehicleEntity1.getIllegalNumber());
            //设置采集时间
            illegalVehicle.setCollectionTime(DateUtil.calendfirstDay());
            //设置添加时间
            illegalVehicle.setAddTime(new Date());
            //设置序号
            illegalVehicle.setRanking(illegalVehicleEntity1.getRanking());
            illegalVehicleDao.save(illegalVehicle);
        }

        logger.info("rankDataTask.dataFetch结束同步" + LocalTime.now());
    }

    private List<RoadAccidentsEntity> changeAreaName(List<RoadAccidentsEntity> list){
      for (RoadAccidentsEntity roadAccidentsEntity :list){
          if ("370481000000".equals(roadAccidentsEntity.getDistrictName())){
              roadAccidentsEntity.setAreaName("滕州市");
              continue;
          }
          if ("370402000000".equals(roadAccidentsEntity.getDistrictName())){
              roadAccidentsEntity.setAreaName("市中区");
              continue;
          }
          if ("370403000000".equals(roadAccidentsEntity.getDistrictName())){
              roadAccidentsEntity.setAreaName("薛城区");
              continue;
          }
          if ("370404000000".equals(roadAccidentsEntity.getDistrictName())){
              roadAccidentsEntity.setAreaName("峄城区");
              continue;
          }
          if ("370405000000".equals(roadAccidentsEntity.getDistrictName())){
              roadAccidentsEntity.setAreaName("台儿庄区");
              continue;
          }
          if ("370406000000".equals(roadAccidentsEntity.getDistrictName())){
              roadAccidentsEntity.setAreaName("山亭区");
              continue;
          }
      }
      return list;
    }

    private List<IllegalCrossingEntity> changeAreaCode(List<IllegalCrossingEntity> list){
        for (IllegalCrossingEntity illegalCrossingEntity:list){
            if ("370481000000".equals(illegalCrossingEntity.getAreaCode())){
                illegalCrossingEntity.setAreaName("滕州市");
                continue;
            }
            if ("370402000000".equals(illegalCrossingEntity.getAreaCode())){
                illegalCrossingEntity.setAreaName("市中区");
                continue;
            }
            if ("370403000000".equals(illegalCrossingEntity.getAreaCode())){
                illegalCrossingEntity.setAreaName("薛城区");
                continue;
            }
            if ("370404000000".equals(illegalCrossingEntity.getAreaCode())){
                illegalCrossingEntity.setAreaName("峄城区");
                continue;
            }
            if ("370405000000".equals(illegalCrossingEntity.getAreaCode())){
                illegalCrossingEntity.setAreaName("台儿庄区");
                continue;
            }
            if ("370406000000".equals(illegalCrossingEntity.getAreaCode())){
                illegalCrossingEntity.setAreaName("山亭区");
                continue;
            }
        }
        return  list;
    }
}
